package gov.nhso.prov.acc.apps.acc6;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;
import com.isud.paging.PageBean;

import gov.nhso.prov.acc.beans.Acc6CrossNegativeYearBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.Utility;

public class ACC60111 extends Utility {
	public String url = "/apps/acc6/ACC60111A.jsp";
	public ACC60111(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = this.getAttr(request, "job");
		if (job.equals("init")) {
			this.init(request);
		} else if (job.equals("add")) {
			this.add(request);
		} else if(job.equals("back")){
			if (this.getLastRequest(request, "lastSearch")) {
				this.search(request);
			} else {
				this.init(request);
			}
		} else if (job.equals("save")) {
			this.save(request);
		} else if (job.equals("edit")) {
			this.edit(request);
		} else if (job.equals("search")){
			this.setLastRequest(request, "lastSearch");
			this.search(request);
		} else if (job.equals("view")) {
			this.view(request);
		} else if (job.equals("del")) {
			this.del(request);
		}
	}
	private String init(HttpServletRequest request){
		return this.url;
	}
	private String add(HttpServletRequest request){
		this.url = "/apps/acc6/ACC60111B.jsp";
		request.setAttribute("job", "save");
		return this.url;
	}
	private String save(HttpServletRequest request){
 		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc6CrossNegativeYearBean cny = new Acc6CrossNegativeYearBean();
			this.beanUtil().copyRequest2Bean(request, cny);
			dao.insert(cny);
			request.setAttribute("code", cny.getCode());
			request.setAttribute("job", "view");
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.view(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String view(HttpServletRequest request){
 		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			String code = this.getAttr(request, "code");
			Acc6CrossNegativeYearBean cny = new Acc6CrossNegativeYearBean();
			cny.setCode(code);
			dao.select(cny);
			request.setAttribute("cny", cny);
			request.setAttribute("job", "edit");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc6/ACC60111B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String edit(HttpServletRequest request){
 		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc6CrossNegativeYearBean cny = new Acc6CrossNegativeYearBean();
			this.beanUtil().copyRequest2Bean(request, cny);
			String oldCode = this.getAttr(request, "oldCode");
			dao.update(cny , " CODE = '" + oldCode + "' ");
			request.setAttribute("cny", cny);
			request.setAttribute("job", "edit");
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc6/ACC60111B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String search(HttpServletRequest request){
 		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc6CrossNegativeYearBean cny = new Acc6CrossNegativeYearBean();
			PageBean pageBean = new PageBean();
			this.beanUtil().copyRequest2Bean(request, pageBean);
			if (pageBean.getOrderField().equals("UPDATED_DATE")) {
				pageBean.setOrderField("CODE");
			}
			this.beanUtil().copyRequest2Bean(request, cny);
			pageBean = dao.selectPage(cny, "", pageBean);
			if (pageBean == null) {
				this.message(request).setMsg("I", "ไม่พบข้อมูล");
			}
			request.setAttribute("pageBean", pageBean);
			request.setAttribute("job", "search");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String del(HttpServletRequest request){
 		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			String code = this.getAttr(request, "code");
			dao.execDataSql("DELETE FROM " + this.schema + ".ACC6_CROSS_NEGATIVE_YEAR WHERE CODE = '" + code + "' ");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				request.setAttribute("code", "");
				this.search(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
}